package com.atguigu.common.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 * 自定义校验注解的校验器
 *
 * @author wu on 2020/11/28 0028
 */
public class ListValueConstraintValidator implements ConstraintValidator<ListVale, Integer> {
    private Set<Integer> setValue = new HashSet<Integer>();
    /**
     * @param * @param null
     * @Date:21:45 2020/11/28 0028
     * @Description:初始化方法
     * @retrun
     */
    public void initialize(ListVale constraintAnnotation) {
        int[] values = constraintAnnotation.vals();
        if (values.length > 0) {
            for (Integer value : setValue) {
                setValue.add(value);
            }
        }
    }
    /**
     * @param * value 需要校验的值
     * @Date:21:45 2020/11/28 0028
     * @Description: 判断是否校验成功
     * @retrun
     */
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        return setValue.contains(value);
    }
}
